﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 Simple.SnowFlake.Time
 * 接口名称 TimeExtension
 * 开发人员：11920
 * 创建时间：2023/7/13 13:51:35
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace Simple.Tools
{
	public static class DateTimeExtension
	{

		/// <summary>
		/// 默认41位
		/// 年-10位2047
		/// 月-4位 12
		/// 日-4位 31
		/// 时-4位 24
		/// 分钟-5位 60
		/// 秒-5位 60
		/// 毫秒-9位 1000
		/// 每个位占用长度
		/// </summary>
		/// <returns></returns>
		/// 
		//64-1
		private static int DefaultLength=43;
		private static int YearLength =12;
		private static int MonthLength =4;
		private static int DayLength =4;
		private static int HourLength =4;
		private static int MinuteLength =5;
		private static int SecondLength =5;
		private static int MillisecondLength = 9;

		/// <summary>
		/// 各个位需要左移的值
		/// </summary>
		private static int LeftMoveYearLength;
		private static int LeftMoveMonthLength;
		private static int LeftMoveDayLength;
		private static int LeftMoveHourLength;
		private static int LeftMoveMinuteLength;
		private static int LeftMoveSecondLength;

		static DateTimeExtension()
		{
			GetMoveLength();
		}

		private static void GetMoveLength()
		{
			LeftMoveYearLength = DefaultLength - YearLength;
			LeftMoveMonthLength = LeftMoveYearLength - MonthLength;
			LeftMoveDayLength = LeftMoveMonthLength - DayLength;
			LeftMoveHourLength = LeftMoveDayLength - HourLength;
			LeftMoveMinuteLength = LeftMoveHourLength - MinuteLength;
			LeftMoveSecondLength = LeftMoveMinuteLength - SecondLength;
		}

		/// <summary>
		/// 设置年的长度
		/// 默认是10位
		/// 最大值1023
		/// </summary>
		/// <param name="i"></param>
		public static void SetYearLength(int i)
		{
			DefaultLength += i - YearLength;
			YearLength = i;
			GetMoveLength();
		}
		public static long GetTimeTicks(this DateTime time)
		{
			return (long)time.Year << LeftMoveYearLength | (long)time.Month << LeftMoveMonthLength | (long)time.Day << LeftMoveDayLength | (long)time.Hour << LeftMoveHourLength | (long)time.Minute << LeftMoveMinuteLength | (long)time.Second << LeftMoveSecondLength| (long)time.Millisecond;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="time"></param>
		/// <param name="offset"></param>
		/// <returns></returns>
		public static long GetTimeTicks(this DateTime time,DateTime offset)
		{
			long year = time.Year - offset.Year;
			long month = time.Month - offset.Month;
			long day = time.Day - offset.Day;
			long hour = time.Hour - offset.Hour;
			long minute = time.Minute - offset.Minute;
			long second = time.Second - offset.Second;
			long millisecond = time.Millisecond - offset.Millisecond;

			return year << LeftMoveYearLength | 
				month << LeftMoveMonthLength | 
				day << LeftMoveDayLength |
				hour << LeftMoveHourLength | 
				minute << LeftMoveMinuteLength | second << LeftMoveSecondLength | millisecond;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public static long GetTimeTicks()
		{
			return (long)DateTime.Now.Year << LeftMoveYearLength | (long)DateTime.Now.Month << LeftMoveMonthLength | (long)DateTime.Now.Day << LeftMoveDayLength | (long)DateTime.Now.Hour << LeftMoveHourLength | (long)DateTime.Now.Minute << LeftMoveMinuteLength | (long)DateTime.Now.Second << LeftMoveSecondLength | (long)DateTime.Now.Millisecond;
		}
		/// <summary>
		/// 2023-04-05 14:30:15
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static string ToDefaultString(this DateTime time)
		{
			return  time.ToString("yyyy-MM-dd HH:mm:ss");
		}

		/// <summary>
		/// Tuesday, April 05, 2023 2:30 PM
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static string TofString(this DateTime time )
		{
			return time.ToString("f");
		}

		/// <summary>
		///  Tuesday, April 05, 2023 2:30:15 PM
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static string ToFString(this DateTime time)
		{
			return time.ToString("F");
		}

		/// <summary>
		///  4/5/2023 2:30 PM
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static string TogString(this DateTime time)
		{
			return time.ToString("g");
		}

		/// <summary>
		///   4/5/2023 2:30:15 PM
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static string ToGString(this DateTime time)
		{
			return time.ToString("G");
		}

		/// <summary>
		///   Tue, 05 Apr 2023 14:30:15 GMT
		/// </summary>
		/// <param name="time"></param>
		/// <returns></returns>
		public static string ToRString(this DateTime time)
		{
			return time.ToString("r");
		}

		/// <summary>
		///  取出年月日 去掉时分秒
		/// </summary>
		/// <returns></returns>
		public static DateTime GetDateDay(this DateTime now)
		{
			return new DateTime(now.Year,now.Month,now.Day);
		}

		/// <summary>
		/// 获取这一天最后时刻
		/// </summary>
		/// <param name="now"></param>
		/// <returns></returns>
		public static DateTime GetCurrentDayLast(this DateTime now)
		{
			DateTime time = new DateTime(now.Year,now.Month,now.Day,23,59,59);
			return time;
		}

	}
}
